package io.renren.modules.app.controller;

import com.itextpdf.text.DocumentException;
import io.renren.common.utils.R;
import io.renren.modules.PCguanli.entity.QuestionEntity;
import io.renren.modules.app.dao.RpExamDao;
import io.renren.modules.app.dao.UserExamDao;
import io.renren.modules.app.entity.Response;
import io.renren.modules.app.entity.RpExamEntity;
import io.renren.modules.app.entity.UserExamEntity;
import io.renren.modules.app.service.RpExamService;
import io.renren.modules.app.service.UserExamService;
import io.renren.modules.app.user.Exam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.*;
import org.springframework.web.bind.annotation.*;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDPageContentStream;
import org.apache.pdfbox.pdmodel.font.PDType1Font;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;

import java.io.IOException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * ClassName: UserExamController
 * Package: io.renren.modules.app.controller
 * Description:
 * 考试相关
 *
 * @Author zzy
 * @Create 2024/4/17 22:13
 * @Version 1.0
 */
@RestController
@RequestMapping("app/userExam")
public class RpExamController {

    @Autowired
    private RpExamDao rpExamDao;
    @Autowired
    private RpExamService rpExamService;

    @GetMapping("/rprandom")
    public Response getRpRandomExamQuestions() {
        Exam examData = rpExamService.getRpRandomExamQuestions();
        List<QuestionEntity> questions = examData.getQuestions();
        if (questions.isEmpty()) {
            return new Response(404, "无内容", null);
        } else {
            return new Response(0, "成功", examData);
        }
    }

    @PostMapping("/Rpupsert")
    public Response upsert(@RequestBody RpExamEntity rpExamEntity) {
        try {
            boolean result = rpExamService.upsertRp(rpExamEntity);
            if (result) {
                return new Response(0, "操作成功", null);
            } else {
                return new Response(404, "操作失败", null);
            }
        } catch (Exception e) {
            return new Response(500, "出现错误：" + e.getMessage(), null);
        }
    }

    @GetMapping("/RpExamRecords")
    public Response getUserExamRecords(@RequestParam int userId) {
        List<RpExamEntity> records = rpExamService.getRecordsByUserId(userId);
        return new Response(0, "成功", records);
    }
    @PostMapping("/RplatestRecords")
    public R userlatestRecords(@RequestParam int userId) {
        RpExamEntity rpExamEntity = rpExamDao.findByuserId(userId);
        System.out.println(rpExamEntity);
        Map<String, Object> map = new HashMap<>();
        if(rpExamEntity==null){
            map.put("value", null);
            map.put("score",null);
            return R.error("没有考试记录").put("data", map);
        }
        map.put("value", rpExamEntity.getValue());
        map.put("score",rpExamEntity.getScore());
        return R.ok("success").put("data", map);
    }

    @GetMapping("/Rpdownload")
    public void downloadFile(Integer num, HttpServletResponse response) throws DocumentException, IOException {
        // 文件内容
        rpExamService.downloadFile(num, response);
    }
}
